package com.base.cn.platform.os.controller.web.qa.question;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.count.PageFrom;
import com.base.cn.platform.os.common.utils.count.PageKeyUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.qa.qaattend.QaAttendService;
import com.base.cn.platform.os.service.course.qa.qareply.QaReplyService;
import com.base.cn.platform.os.service.course.qa.qatag.QaTagService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.exam.question.ExamQuestionsService;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import com.base.cn.platform.os.service.manage.op.advert.OpAdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台问答Controller
 *
 * @author LIZHIPENG
 * @create 2018-05-17
 */
@Controller
@RequestMapping("/web/qa")
public class WebQuestionController extends WebBaseController {

    @Autowired
    private QaQuestionService questionService;
    @Autowired
    private QaTagService qaTagService;
    @Autowired
    private QaReplyService qaReplyService;
    @Autowired
    private QaAttendService qaAttendService;
    @Autowired
    private ExamQuestionsService examQuestionsService;
    @Autowired
    private PageViewCountService pageViewCountService;

    @Autowired
    private OpAdvertService opAdvertService;

    /**
     * 初始化问答广场页面
     * @param model
     * @return
     */
    @RequestMapping("/listQuestion")
    public String listQuestion(Model model){
        //是否显示banner图
        model.addAttribute("banner", true);
        pageViewCountService.addPageCount(PageKeyUtils.QA_INDEX, PageFrom.PAGE_FROM_PC);
        return resultPage("/qa/question/qa-question-list");
    }

    /**
     * 初始化关键字搜索问题页面
     * @return
     */
    @RequestMapping("/listQuestionByKeyWord")
    public String listQuestionByKeyWord(){
        return resultPage("/qa/question/qa-question-keyword");
    }

    /**
     * ajax加载问题列表数据
     * @param model
     * @param currentPage 页码
     * @param params 条件
     * @return
     */
    @RequestMapping("/ajaxListQuestion")
    public String ajaxListQuestion(Model model, HttpServletRequest request,
                                   @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                   @RequestBody Map<String, Object> params){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("selectFrom",2);
        if(ObjectUtils.isNotEmpty(params.get("isFree"))&&params.get("isFree").equals("1")){
            params.put("notNoFree",true);
            params.remove("isFree");
        }
        Map<String, Object> dataMap = questionService.queryQuestionListPage(currentPage, 10, params, userId,false, false, true, false, false, true, false,false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/qa/question/ajax-qa-question-list");
    }

    /**
     * 获取问答广场右侧页面内容
     * @return
     */
    @RequestMapping("/getRightContent")
    public String getRightContent(Model model){
        Map<String, Object> params = new HashMap<>();
        //问题数量
        BigDecimal questionCount = questionService.questionCount(params);
        model.addAttribute("questionCount", questionCount);
        //评论数量
        BigDecimal replyCount = qaReplyService.replyCount(params);
        model.addAttribute("replyCount", replyCount);
        //围观数量
        BigDecimal attendCount = qaAttendService.attendCount(params);
        model.addAttribute("attendCount", attendCount);
        //标签列表
        params.put("status",1);
        List<Map<String, Object>> tagList = qaTagService.queryQaTagList(params);
        model.addAttribute("tagList", tagList);
        Map<String,Object> condition = new HashMap<>();
        condition.put("status",1);
        BigDecimal examCount = examQuestionsService.getQuestionCount(condition);
        model.addAttribute("examCount", examCount);
        return resultPage("/qa/question/ajax-qa-question-right");
    }

    /**
     * 查看问题详情
     * @param model
     * @param questionId 问题ID
     * @return
     */
    @RequestMapping("/questionInfo")
    public String questionInfo(Model model,
                               HttpServletRequest request,
                               @RequestParam("questionId")BigDecimal questionId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);//当前登录用户ID
        Map<String, Object> question = questionService.findQuestionByIdForFront(questionId, userId,false,true,true,true,true,true, false);
        model.addAttribute("question", question);
        return resultPage("/qa/question/qa-question-info");
    }

    /**
     * 通过套餐ID查询课程列表
     * @param packageId 套餐ID
     * @return
     */
    @RequestMapping("/getCourseByPackageId")
    @ResponseBody
    public Map<String, Object> getCourseListByPackageId(@RequestParam("packageId") BigDecimal packageId){
        return questionService.getCourseListByPackageId(packageId);
    }

    /**
     * 根据课程ID查询章节列表
     * @param courseId 课程ID
     * @return
     */
    @RequestMapping("/getCourseCatalogByCourseId")
    @ResponseBody
    public Map<String, Object> getCourseCatalogByCourseId(@RequestParam("courseId") BigDecimal courseId){
        Map<String, Object> catalogMap = questionService.getCourseCatalogByCourseId(courseId);
        return catalogMap;
    }

    /**
     * ajax加载问题评论列表
     * @param model
     * @param currentPage
     * @param params
     * @return
     */
    @RequestMapping("/reply/ajaxQueryQaReplyList")
    public String ajaxQueryQaReplyList(Model model,
                                       @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                       @RequestBody Map<String, Object> params){
        params.put("parentId",0);
        params.put("status",1);
        Map<String, Object> dataMap = qaReplyService.findQaReplyListPage(currentPage, 10, params, true, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/qa/question/ajax-qa-question-reply-list");
    }

    /**
     * 获取广告图片
     * @return
     */
    @RequestMapping("/getAdvertList")
    @ResponseBody
    public Map<String,Object> getAdvertList(){
        Map<String, Object> params = new HashMap<>();
        params.put("placeKey", "QUESTIONINDEX");
        params.put("status",1);
        List<Map<String, Object>> advertList = opAdvertService.queryAdvertList(params, false, false, false);
        return ResultUtil.SUCCESS(advertList);
    }
}
